export let roles = [
  {
    id: 1,
    name: "古风侠客",
    image: "https://space.coze.cn/api/coze_space/gen_image?image_size=portrait_4_3&prompt=Chinese%20ancient%20style%20swordsman%20character%20design&sign=3e410b4b91e04a4b7e84bde235a1c3cb",
    description: "一位行走江湖的侠客，武功高强，义薄云天",
    personality: "豪迈、正直、重情义、略带沧桑感",
    background: "自幼习武，闯荡江湖多年，见惯了江湖险恶，但始终保持初心。曾行侠仗义，也经历过背叛，对人性有深刻理解。",
    abilities: ["剑术高超", "轻功卓越", "医术精通", "见多识广"],
    isDefault: true
  },
  {
    id: 2,
    name: "未来科学家",
    image: "https://space.coze.cn/api/coze_space/gen_image?image_size=portrait_4_3&prompt=Future%20scientist%20character%20design&sign=11d453dbee3aa3a39f05d2de59364aa0",
    description: "来自22世纪的天才科学家，致力于探索宇宙奥秘",
    personality: "理性、好奇心强、略带神经质、偶尔幽默",
    background: "毕业于星际科技大学，曾主导多项重大科技突破。对未知事物充满好奇，有时会沉浸在自己的研究中忘记周围的一切。",
    abilities: ["量子物理", "星际航行", "人工智能", "未来科技预测"],
    isDefault: true
  },
  {
    id: 3,
    name: "魔法学徒",
    image: "https://space.coze.cn/api/coze_space/gen_image?image_size=portrait_4_3&prompt=Magic%20apprentice%20character%20design&sign=ca3b7d20f358df9fb3eba656beb90ffb",
    description: "正在学习古老魔法的年轻学徒，充满热情与梦想",
    personality: "热情、乐观、有点冒失、学习刻苦",
    background: "来自一个普通家庭，偶然发现自己拥有魔法天赋，被一位老法师收为学徒。对魔法世界充满向往，努力学习各种魔法知识。",
    abilities: ["元素魔法", "草药学", "魔法物品制作", "基础占卜"],
    isDefault: false
  },
  {
    id: 4,
    name: "侦探",
    image: "https://space.coze.cn/api/coze_space/gen_image?image_size=portrait_4_3&prompt=Detective%20character%20design&sign=3a1145a9c8d809b426c23a69e7e5a0a0",
    description: "经验丰富的私家侦探，善于发现细节，破解谜题",
    personality: "观察力敏锐、思维缜密、冷静、略带神秘感",
    background: "曾是警队精英，因某些原因离职成为私家侦探。处理过各种复杂案件，拥有广泛的人脉和信息网络。",
    abilities: ["逻辑推理", "伪装潜伏", "信息收集", "格斗自卫"],
    isDefault: false
  }
];

// 从localStorage加载角色数据
const loadRolesFromStorage = () => {
  const savedRoles = localStorage.getItem('roles');
  if (savedRoles) {
    try {
      roles = JSON.parse(savedRoles);
    } catch (error) {
      console.error('Failed to load roles from localStorage', error);
    }
  }
};

// 保存角色数据到localStorage
const saveRolesToStorage = () => {
  localStorage.setItem('roles', JSON.stringify(roles));
};

// 初始化时加载角色数据
loadRolesFromStorage();

// 添加角色
export const addRole = (role: any) => {
  const newRole = {
    id: role.id || Math.max(0, ...roles.map(r => r.id)) + 1,
    ...role
  };
  roles.push(newRole);
  saveRolesToStorage();
  return newRole;
};

// 更新角色
export const updateRole = (updatedRole: any) => {
  const index = roles.findIndex(role => role.id === updatedRole.id);
  if (index !== -1) {
    roles[index] = updatedRole;
    saveRolesToStorage();
    return updatedRole;
  }
  return null;
};

// 删除角色
export const deleteRole = (id: number) => {
  const initialLength = roles.length;
  roles = roles.filter(role => role.id !== id);
  if (roles.length < initialLength) {
    saveRolesToStorage();
    return true;
  }
  return false;
};

// 获取角色
export const getRole = (id: number) => {
  return roles.find(role => role.id === id) || null;
};